#include "app.h"
#include "MPU6050.h"
sensor_property_typeDef sensor_data;
gyro_typedef gyro;
accel_typedef accel;
posture_typedef posture;
//APP 
void sensor_scan(void){
    
    IIC_SCAN(IIC0,OPT3001_ADDR1);
    IIC_SCAN(IIC0,OPT3001_ADDR2);
    IIC_SCAN(IIC0,OPT3001_ADDR3);
    IIC_SCAN(IIC0,OPT3001_ADDR4);

    IIC_SCAN(IIC1,OPT3001_ADDR1);
    IIC_SCAN(IIC1,OPT3001_ADDR2);
    IIC_SCAN(IIC1,OPT3001_ADDR3);
    IIC_SCAN(IIC1,OPT3001_ADDR4);

    IIC_SCAN(IIC2,OPT3001_ADDR1);
    IIC_SCAN(IIC2,OPT3001_ADDR2);
    IIC_SCAN(IIC2,OPT3001_ADDR3);
    IIC_SCAN(IIC2,OPT3001_ADDR4);

    IIC_SCAN(IIC3,OPT3001_ADDR1);
    IIC_SCAN(IIC3,OPT3001_ADDR2);
    IIC_SCAN(IIC3,OPT3001_ADDR3);
    IIC_SCAN(IIC3,OPT3001_ADDR4);

}
void get_sensor_data(sensor_property_typeDef* data){
    
    //IIC 0
    calculate_lux(IIC0,OPT3001_ADDR4,&data->data[0],&data->hex_data[0]);
    calculate_lux(IIC0,OPT3001_ADDR3,&data->data[1],&data->hex_data[1]);
    calculate_lux(IIC0,OPT3001_ADDR2,&data->data[2],&data->hex_data[2]);
    calculate_lux(IIC0,OPT3001_ADDR1,&data->data[3],&data->hex_data[3]);
    //IIC 1
    calculate_lux(IIC1,OPT3001_ADDR1,&data->data[4],&data->hex_data[4]);
    calculate_lux(IIC1,OPT3001_ADDR2,&data->data[5],&data->hex_data[5]);
    calculate_lux(IIC1,OPT3001_ADDR3,&data->data[6],&data->hex_data[6]);
    calculate_lux(IIC1,OPT3001_ADDR4,&data->data[7],&data->hex_data[7]);
    //IIC 2
    calculate_lux(IIC2,OPT3001_ADDR4,&data->data[8],&data->hex_data[8]);
    calculate_lux(IIC2,OPT3001_ADDR3,&data->data[9],&data->hex_data[9]);
    calculate_lux(IIC2,OPT3001_ADDR2,&data->data[10],&data->hex_data[10]);
    calculate_lux(IIC2,OPT3001_ADDR1,&data->data[11],&data->hex_data[11]);
    //IIC 3
    calculate_lux(IIC3,OPT3001_ADDR1,&data->data[12],&data->hex_data[12]);
    calculate_lux(IIC3,OPT3001_ADDR2,&data->data[13],&data->hex_data[13]);
    calculate_lux(IIC3,OPT3001_ADDR3,&data->data[14],&data->hex_data[14]);
    calculate_lux(IIC3,OPT3001_ADDR4,&data->data[15],&data->hex_data[15]);
}

void vofa_send(sensor_property_typeDef* sensor)
{
	printf("val:");
	for(u8 j=0;j<Spec_channel*2-1;j++)
	{
		printf("%.1f,",sensor->data[j]);
	}
	printf("%.1f,",sensor->data[Spec_channel*2-1]);
	printf("\r\n");
}
void float_send(sensor_property_typeDef* sensor)
{
	for(u8 j=0;j<Spec_channel*2;j++)
	{
		printf("%.0f,",sensor->data[j]);
	}
	printf("\r\n");
}
void send_position(posture_typedef* posture){
	//Z data make nosense
	printf("%.2f,%.2f\r\n",posture->x,posture->y);
}
void USART_SendChar(u8 data) 
{  
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){;}
    USART_SendData(USART1, data);  
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){;}
} 
void uart_Send_u16(u16 data){
    u8 dat[2];
    dat[0]=(data)>>8;//H8
    dat[1]=(data)& 0x00FF;//L8
    USART_SendChar(dat[0]);
    USART_SendChar(dat[1]);
}
void hex_send(sensor_property_typeDef* sensor)
{

	for(u8 j=0;j<=Spec_channel*2-1;j++)
	{
		//printf("%#x,",);
        //USART_SendData(USART1,sensor->hex_data[j]);
        uart_Send_u16(sensor->hex_data[j]);
	}
	//printf("%#x\r\n",sensor->hex_data[Spec_channel*2-1]);
}

void OPT3002_Sync(void){
    //IIC 0
    opt3001_init(IIC0,OPT3001_ADDR1);
    opt3001_init(IIC0,OPT3001_ADDR2);
    opt3001_init(IIC0,OPT3001_ADDR3);
    opt3001_init(IIC0,OPT3001_ADDR4);
     //IIC 1
    opt3001_init(IIC1,OPT3001_ADDR1);
    opt3001_init(IIC1,OPT3001_ADDR2);
    opt3001_init(IIC1,OPT3001_ADDR3);
    opt3001_init(IIC1,OPT3001_ADDR4);
     //IIC 2
    opt3001_init(IIC2,OPT3001_ADDR1);
    opt3001_init(IIC2,OPT3001_ADDR2);
    opt3001_init(IIC2,OPT3001_ADDR3);
    opt3001_init(IIC2,OPT3001_ADDR4);
     //IIC 3
    opt3001_init(IIC3,OPT3001_ADDR1);
    opt3001_init(IIC3,OPT3001_ADDR2);
    opt3001_init(IIC3,OPT3001_ADDR3);
    opt3001_init(IIC3,OPT3001_ADDR4);

}
void once_measure(void){
	static int count = 0;
    printf("\r\n");
    printf("%d,\r\n",count++);
    OPT3002_Sync();
    delay_ms(1000);
    get_sensor_data(&sensor_data);
    vofa_send(&sensor_data);
	
    delay_ms(1000);
    get_sensor_data(&sensor_data);
    vofa_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    vofa_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    vofa_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    vofa_send(&sensor_data);
    //hex_send(&sensor_data);
}
void m_hex(void){
    // char show_buff[20];
	  //static int count = 0;
    //printf("\r\n");
    //printf("%d,\r\n",count++);
    OPT3002_Sync();

    delay_ms(1000);
    get_sensor_data(&sensor_data);
    hex_send(&sensor_data);
    printf("\r\n");
	return ;
    delay_ms(1000);
    get_sensor_data(&sensor_data);
    hex_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    hex_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    hex_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    hex_send(&sensor_data);
    printf("\r\n");
}

void m_float(void){
	LED0=1;
    // char show_buff[20];
	  //static int count = 0;
    //printf("\r\n");
    //printf("%d,\r\n",count++);
    OPT3002_Sync();
    
    delay_ms(1000);
    get_sensor_data(&sensor_data);
    float_send(&sensor_data);
    //printf("\r\n");
	 
	return ;
    delay_ms(1000);
    get_sensor_data(&sensor_data);
    float_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    float_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    float_send(&sensor_data);
	
	delay_ms(1000);
    get_sensor_data(&sensor_data);
    float_send(&sensor_data);
    //printf("\r\n");
}
void pos_test(void){
	int  i= 5000;
	float count =0;
	float angle_x_sum;
	float angle_y_sum;
	LED0=1;
	while(i--){
        Read_MPU6050(&accel,&gyro,&posture);
        send_position(&posture);
        delay_ms(1);
	}
}
void m_pos(void){
  int  i= 15;
	float count =0;
	float angle_x_sum;
	float angle_y_sum;
	LED0=1;
	while(i--){
        count++;
        Read_MPU6050(&accel,&gyro,&posture);
        angle_x_sum+=posture.x;
        angle_y_sum+=posture.y;
        delay_ms(5);
	}
	posture.x=angle_x_sum/count;
	posture.y=angle_y_sum/count;
	send_position(&posture);
}
void opt_live_watch(void){
    OPT3002_Sync();
    while(1){
        LED0=!LED0;
        delay_ms(1000);
        get_sensor_data(&sensor_data);
        vofa_send(&sensor_data);
    }
}
//===================end of app ========================
